package org.fireflow2.designer.eclipse.simulation;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.gef.ui.parts.SelectionSynchronizer;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IFileEditorInput;
import org.fireflow.engine.entity.runtime.ActivityInstance;
import org.fireflow.engine.entity.runtime.ProcessInstance;
import org.fireflow.pdl.fpdl20.process.Activity;
import org.fireflow.pdl.fpdl20.process.WorkflowProcess;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.commands.service.java.JavaServiceCreationWizard;
import org.fireflow2.designer.eclipse.editors.FireflowMultiPageEditor;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.parts.PartFactory;
import org.fireflow2.designer.eclipse.resources.Version;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;
//import org.fireflow2.designer.simulation.FireflowSimulator;
//import org.fireflow2.designer.simulation.persistence.IStorageChangeListener;
//import org.fireflow2.designer.simulation.persistence.StorageChangedEvent;

public class SimulatorPanel extends GraphicalEditor {
//	implements	IStorageChangeListener {
	public static final int RUNNING = 1;
	public static final int WAITING = 2;
	public static final int COMPLETED = 3;
	public static final int INITSTATE = 0;

	public static Map<String, Color> SimulationColorMap = new HashMap<String, Color>();
	static {
		SimulationColorMap.put(
				AbsWrapper.SIMULATION_PROPERTY_COMPLETED, new Color(
						null, 0, 0, 255));
		SimulationColorMap.put(
				AbsWrapper.SIMULATION_PROPERTY_RUNNING, new Color(
						null, 0, 255, 0));
		SimulationColorMap.put(
				AbsWrapper.SIMULATION_PROPERTY_WAITING, new Color(
						null, 255, 180, 57));
		SimulationColorMap.put(
				AbsWrapper.SIMULATION_PROPERTY_ORIGINAL, new Color(
						null, 0, 0, 0));
		SimulationColorMap.put(
				AbsWrapper.SIMULATION_PROPERTY_INITIALIZED,
				new Color(null, 255, 0, 255));

	}

	SubProcessWrapper mainWorkflowProcessWrapper = null;
	SubProcessWrapper currerntWorkflowProcessWrapper = null;
	ProcessInstance currentProcessInstance = null;
	List subWorkflowProcessWrapperList = new ArrayList();
	SimulatorToolBar toolbar = null;
	FireflowMultiPageEditor parentEditor = null;
	// OutlinePage outlinePage = null;
	// PaletteRoot paletteRoot = null;

	Map process_processInstance_map = new HashMap();

//	FireflowSimulator simulator = null;

	public SimulatorPanel(SubProcessWrapper processWrapper,
			FireflowMultiPageEditor pedt) {
		super();

		setEditDomain(new DefaultEditDomain(this));
		mainWorkflowProcessWrapper = processWrapper;

		parentEditor = pedt;

//		simulator = FireflowSimulator.getCurrentSimulator();

	}

	public void putProcessInstance(String processId,
			ProcessInstance processInstance) {
		process_processInstance_map.put(processId, processInstance);
	}

	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		getGraphicalViewer()
				.setRootEditPart(new ScalableFreeformRootEditPart());
		getGraphicalViewer().setEditPartFactory(new PartFactory(false));
	}

	protected SimulatorToolBar createToolbar(final Composite parent) {
		SimulatorToolBar toolbar = new SimulatorToolBar(parent, this);

		return toolbar;

	}

	public ISelection getSelection() {
		return this.getGraphicalViewer().getSelection();
	}

	@Override
	protected void initializeGraphicalViewer() {
		// TODO Auto-generated method stub
		this.getGraphicalViewer().setContents(mainWorkflowProcessWrapper);
	}

	public void createPartControl(Composite parent) {
		// datafieldsWrapper.setLayout(new FillLayout());
		Composite composite = new Composite(parent, SWT.NO_FOCUS);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 2;
		layout.marginHeight = 1;
		layout.verticalSpacing = 2;

		composite.setLayout(layout);
		toolbar = createToolbar(composite);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;

		toolbar.getControl().setLayoutData(gridData);

		createGraphicalViewer(composite);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		this.getGraphicalViewer().getControl().setLayoutData(gridData);

		// Splitter splitter = new Splitter(datafieldsWrapper, SWT.HORIZONTAL);
		// Control toolbar = createToolbar(splitter);
		// createGraphicalViewer(splitter);
		// splitter.maintainSize(toolbar);
		// splitter.setFixedSize(25);
		this.getGraphicalViewer().addSelectionChangedListener(toolbar);

		this.addSubWorkflowProcessWrapper(this.mainWorkflowProcessWrapper);

	}

	@Override
	public Object getAdapter(Class type) {
		if (type == SelectionSynchronizer.class) {
			return this.getSelectionSynchronizer();
		}
		if (type == EditDomain.class) {
			return this.getEditDomain();
		}
		return super.getAdapter(type);
	}

	@Override
	public void doSave(IProgressMonitor arg0) {
		// TODO Auto-generated method stub

	}

	public void refresh() {
		this.subWorkflowProcessWrapperList.clear();
		addSubWorkflowProcessWrapper(this.mainWorkflowProcessWrapper);
		// this.subWorkflowProcessWrapperList.add(this.mainWorkflowProcessWrapper);
		// this.toolbar
		// .refreshWorkflowProcessListViewer(this.subWorkflowProcessWrapperList);
		// this.toolbar.setCurrentWorkflowProcessIndex(1);
	}

	public void addSubWorkflowProcessWrapper(
			SubProcessWrapper processWrapper) {
		this.subWorkflowProcessWrapperList.add(processWrapper);
		this.toolbar
				.refreshWorkflowProcessListViewer(this.subWorkflowProcessWrapperList);
		for (int i = 0; i < this.subWorkflowProcessWrapperList.size(); i++) {
			SubProcessWrapper wrapper = (SubProcessWrapper) this.subWorkflowProcessWrapperList
					.get(i);
			if (wrapper.getID().equals(processWrapper.getID())) {
				this.toolbar.setCurrentWorkflowProcessIndex(i);
				break;
			}
		}
		setCurrentWorkflowProcessWrapper(processWrapper);
	}

	public void setCurrentWorkflowProcessWrapper(
			SubProcessWrapper processWrapper) {
		this.getGraphicalViewer().setContents(processWrapper);
		this.currerntWorkflowProcessWrapper = processWrapper;

	}
	/*
	public void onStorageChanged(StorageChangedEvent e) {
		
		// TODO Auto-generated method stub
		WorkflowProcess workflowProcess = (WorkflowProcess) this.currerntWorkflowProcessWrapper
				.getWorkflowModelElement();

		if (e.getEventType() == StorageChangedEvent.UPDATE_DATA
				&& (e.getProcessId() == null || !e.getProcessId().equals(
						workflowProcess.getId()))) {
			return;
		}

		if (e.getEventType() == StorageChangedEvent.CLEAR_DATA
				&& !e.getProcessId().equals(workflowProcess.getId())
				&& !e.getProcessId().equals(StorageChangedEvent.ALL_PROCESS)) {
			return;
		}

		if (e.getEventType() == StorageChangedEvent.CLEAR_DATA) {
			initEditPartColor();
		} else {
			colorEditPart(e.getProcessInstance());
		}
		
	}
*/
	protected void colorEditPart(String processId) {
		this.colorEditPart((ProcessInstance) this.process_processInstance_map
				.get(processId));
	}

	protected void colorEditPart(ProcessInstance processInstance) {
		this.currentProcessInstance = processInstance;
		List traceInfo = null;//this.simulator.findTraceInfo(currentProcessInstance
				//.getId());
		if (traceInfo == null || traceInfo.size() == 0) {
			colorEditPart_Strategy1(currentProcessInstance);
		} else {
			colorEditPart_Strategy2(currentProcessInstance, traceInfo);
		}
	}

	protected void colorTaskEditPart(ActivityInstance taskInst, List wrapperList) {
		/*
		AbsWrapper modelWrapper = null;
		for (int i = 0; wrapperList != null && i < wrapperList.size(); i++) {
			modelWrapper = (AbsWrapper) wrapperList.get(i);
			if (modelWrapper instanceof TaskRefWrapper) {
				TaskRef taskref = (TaskRef) modelWrapper
						.getWorkflowModelElement();
				if (!taskInst.getTaskId().equals(
						taskref.getReferencedTask().getId())) {
					continue;
				}
			} else if (!taskInst.getTaskId().equals(modelWrapper.getID())) {
				continue;
			}

			if (taskInst.getState() == ActivityInstanceState.INITIALIZED
					&& modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_INITIALIZED);

			} else if (taskInst.getState() == ActivityInstanceState.RUNNING
					&& modelWrapper != null) {
				// 说明尚未签收,处于待签收状态
				if (((ActivityInstance) taskInst).getCanBeWithdrawn()) {
					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_INITIALIZED);
				} else {
					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_RUNNING);
				}

			} else {
				if (modelWrapper != null) {
					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);
				}
			}
		}
		*/
	}

	protected void colorEditPart_Strategy2(ProcessInstance processInstance,
			List traceInfo) {
		/*
		WorkflowProcess workflowProcess = (WorkflowProcess) this.currerntWorkflowProcessWrapper
				.getWorkflowModelElement();

		for (int i = 0; i < traceInfo.size(); i++) {
			ProcessInstanceTrace trace = (ProcessInstanceTrace) traceInfo
					.get(i);

			ModelElement fromWfElement = workflowProcess.findWFElementById(trace
					.getFromNodeId());
			ModelElement toWfElement = workflowProcess.findWFElementById(trace
					.getToNodeId());

			String sn = fromWfElement.getSn();
			AbsWrapper modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);
			}

			sn = toWfElement.getSn();
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			if (modelWrapper != null) {
				if (toWfElement instanceof Activity) {
					int activityInstanceState = COMPLETED;

					List<ActivityInstance> taskInstList = simulator
							.findTaskInstancesForProcessInstanceByStepNumber(
									this.currentProcessInstance.getId(), trace
											.getStepNumber());

					for (int m = 0; taskInstList != null
							&& m < taskInstList.size(); m++) {
						ActivityInstance taskInst = taskInstList.get(m);
						if (taskInst.getState() != ActivityInstanceState.COMPLETED
								&& taskInst.getState() != ActivityInstanceState.CANCELED) {
							activityInstanceState = RUNNING;
						}
						colorTaskEditPart(taskInst,
								((ActivityWrapper) modelWrapper).getChildren());
					}

					if (activityInstanceState == RUNNING) {
						modelWrapper
								.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_RUNNING);
					} else {
						modelWrapper
								.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);

					}

				} else {
					if (toWfElement instanceof EndNode) {

						List l = simulator.findTokensForProcessInstance(
								this.currentProcessInstance.getId(),
								toWfElement.getId());
						if (l == null || l.size() == 0) {
							modelWrapper
									.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);
						} else {
							modelWrapper
									.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_WAITING);
						}
					} else {
						modelWrapper
								.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_WAITING);
					}
				}
			}

			String edgeId = trace.getEdgeId();
			ModelElement edge = workflowProcess.findWFElementById(edgeId);

			if (edge != null) {

				sn = edge.getSn();
				modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
						.findModelWrapperBySn(sn);
				if (modelWrapper != null) {

					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);

				}

			}
		}
		*/
	}

	protected void colorEditPart_Strategy1(ProcessInstance processInstance) {
		/*
		WorkflowProcess workflowProcess = (WorkflowProcess) this.currerntWorkflowProcessWrapper
				.getWorkflowModelElement();
		AbsWrapper modelWrapper = null;

		// activity
		List<String> completedTransitionIds = new ArrayList<String>();
		List<Activity> activities = workflowProcess.getActivities();
		for (int i = 0; i < activities.size(); i++) {
			Activity activity = activities.get(i);

			List<ActivityInstance> taskInstList = simulator
					.findTaskInstancesForProcessInstance(
							this.currentProcessInstance.getId(), activity
									.getId());
			if (taskInstList == null || taskInstList.size() == 0)
				continue;

			int state = COMPLETED;
			String sn = activity.getSn();
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			for (int m = 0; m < taskInstList.size(); m++) {
				ActivityInstance taskInst = taskInstList.get(m);

				if (taskInst.getState() != ActivityInstanceState.COMPLETED
						&& taskInst.getState() != ActivityInstanceState.CANCELED) {
					state = RUNNING;
				}
				this.colorTaskEditPart(taskInst,
						((ActivityWrapper) modelWrapper).getChildren());
			}

			if (modelWrapper != null) {
				if (state == COMPLETED) {
					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_COMPLETED);
				} else if (state == RUNNING) {
					modelWrapper
							.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_RUNNING);
				}
			}

			// color the transition

		}
	*/

	}

	private void initEditPartColor() {
		/*
		WorkflowProcess workflowProcess = (WorkflowProcess) this.currerntWorkflowProcessWrapper
				.getWorkflowModelElement();
		// 开始节点
		String startNodeSN = workflowProcess.getStartNode().getSn();
		AbsWrapper modelWrapper = this.currerntWorkflowProcessWrapper
				.findModelWrapperBySn(startNodeSN);
		if (modelWrapper != null)
			modelWrapper
					.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);

		// activity
		String sn = "";
		List<String> completedTransitionIds = new ArrayList<String>();
		List<Activity> activities = workflowProcess.getActivities();
		for (int i = 0; i < activities.size(); i++) {
			Activity activity = activities.get(i);

			List taskList = activity.getTasks();
			for (int j = 0; j < taskList.size(); j++) {
				Task task = (Task) taskList.get(j);
				sn = task.getSn();
				modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
						.findModelWrapperBySn(sn);
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}

			sn = activity.getSn();
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}

			sn = activity.getEnteringTransition().getSn();
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}

			sn = activity.getLeavingTransition().getSn();
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(sn);
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}

		}

		// synchronizer
		List<Synchronizer> synchronizers = workflowProcess.getSynchronizers();
		for (int i = 0; i < synchronizers.size(); i++) {
			Synchronizer synchronizer = synchronizers.get(i);

			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(synchronizer.getSn());
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}
		}

		// endnode
		List<EndNode> endnodes = workflowProcess.getEndNodes();
		for (int i = 0; i < endnodes.size(); i++) {
			EndNode endnode = endnodes.get(i);
			modelWrapper = (AbsWrapper) this.currerntWorkflowProcessWrapper
					.findModelWrapperBySn(endnode.getSn());
			if (modelWrapper != null) {
				modelWrapper
						.fireSimulationPropertyChange(AbsWrapper.SIMULATION_PROPERTY_ORIGINAL);
			}
		}
		 */
	}
}

class VarDialog extends Dialog {
	Text nameText = null;
	Combo dataTypeCombo = null;
	Text valueText = null;

	String varName = "";
	Object varValue = null;

	public VarDialog(Shell shell) {
		super(shell);
	}

	protected Control createDialogArea(Composite parent) {
		// datafieldsWrapper.setLayout(new FillLayout());
		Composite panel = (Composite) super.createDialogArea(parent);
		panel.setLayout(new GridLayout(2, false));

		Label label3 = new Label(panel, SWT.NONE);
		label3.setText("Name:");
		nameText = new Text(panel, SWT.SINGLE | SWT.BORDER);
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		nameText.setLayoutData(gridData);

		Label label1 = new Label(panel, SWT.NONE);
		label1.setText("Data Type:");

		dataTypeCombo = new Combo(panel, SWT.DROP_DOWN);

		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		dataTypeCombo.setItems(new String[] { 
//				DataField.STRING,
//				DataField.BOOLEAN, DataField.INTEGER, DataField.DATETIME,
//				DataField.FLOAT, DataField.DOUBLE, DataField.LONG 
				});

		Label label2 = new Label(panel, SWT.NONE);
		label2.setText("Value:");
		valueText = new Text(panel, SWT.SINGLE | SWT.BORDER);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		valueText.setLayoutData(gridData);

		Label tmp = new Label(panel, SWT.NONE);
		Label tmp2 = new Label(panel, SWT.NONE);
		tmp2.setText("(Date format is yyyyMMdd HHmmss)");
		return panel;

	}

	public String getVarName() {
		return varName;
	}

	public Object getVarValue() {
		return varValue;
	}

	protected void okPressed() {
		if (this.nameText.getText() == null
				|| this.nameText.getText().trim().equals("")) {
			this.nameText.forceFocus();
			return;
		}
		this.varName = this.nameText.getText().trim();
		if (this.valueText.getText() == null
				|| this.valueText.getText().trim().equals("")) {
			this.varValue = null;
		} else {
			this.varValue = createVarValue(this.dataTypeCombo.getText(),
					this.valueText.getText().trim());
		}

		super.okPressed();
	}

	private Object createVarValue(String type, String strValue) {

		/* 05-03
		SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
		if (type.equals(DataField.STRING)) {
			return strValue;
		}
		if (strValue == null || strValue.trim().equals("")) {
			return null;
		}
		if (type.equals(DataField.INTEGER)) {
			return new Integer(strValue);
		} else if (type.equals(DataField.LONG)) {
			return new Long(strValue);
		} else if (type.equals(DataField.FLOAT)) {
			return new Float(strValue);
		} else if (type.equals(DataField.DOUBLE)) {
			return new Double(strValue);
		} else if (type.equals(DataField.BOOLEAN)) {
			return new Boolean(strValue);
		} else if (type.equals(DataField.DATETIME)) {
			Date d = null;
			try {
				d = dFormat.parse(strValue);
			} catch (Exception e) {

			}
			return d;
		}
		*/
		return null;
	}

	protected void cancelPressed() {
		this.varName = null;
		super.cancelPressed();
	}
}

class JumpToDialog extends Dialog {
	ComboViewer targetActivityCombo;
	Text actorIdsText;
	Button needSignButton;
	boolean needSign = false;
	String targetActivityId = "";
	String nextActorIds = "";

	boolean valueChanged = false;

	boolean isLoop = false;

	WorkflowProcess workflowProcess = null;

	public JumpToDialog(Shell shell, WorkflowProcess process, boolean arg_IsLoop) {
		super(shell);
		workflowProcess = process;
		isLoop = arg_IsLoop;
	}

	protected Control createDialogArea(Composite parent) {
		// datafieldsWrapper.setLayout(new FillLayout());
		Composite panel = (Composite) super.createDialogArea(parent);

		panel.setLayout(new GridLayout(2, false));

		Label label1 = new Label(panel, SWT.NONE);
		label1.setText("Target Activity:");

		targetActivityCombo = new ComboViewer(panel, SWT.DROP_DOWN);

		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		targetActivityCombo.getControl().setLayoutData(gridData);
		targetActivityCombo.setContentProvider(new ArrayContentProvider());
		targetActivityCombo.setLabelProvider(new LabelProvider() {
			public String getText(Object element) {
				Activity activity = (Activity) element;
				String name = activity.getName();
				String displayName = activity.getDisplayName();
				if (displayName == null)
					displayName = "";
				return displayName.trim().equals("") ? name : displayName;
			}

		});
		//TODO 0503 注释
//		targetActivityCombo.setInput(workflowProcess.getActivities());
		targetActivityCombo
				.addSelectionChangedListener(new ISelectionChangedListener() {

					public void selectionChanged(SelectionChangedEvent arg0) {
						// TODO Auto-generated method stub
						StructuredSelection selection = (StructuredSelection) arg0
								.getSelection();
						Activity activity = (Activity) selection
								.getFirstElement();
						targetActivityId = activity.getId();
					}

				});

		if (isLoop)
			return panel;
		Label label2 = new Label(panel, SWT.NONE);
		label2.setText("Next Actor Id(comma separated):");
		actorIdsText = new Text(panel, SWT.SINGLE | SWT.BORDER);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		actorIdsText.setLayoutData(gridData);
		actorIdsText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				nextActorIds = actorIdsText.getText();
			}

		});

		Label label3 = new Label(panel, SWT.NONE);
		label3.setText("Need Claim:");
		needSignButton = new Button(panel, SWT.CHECK);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.widthHint = 250;
		needSignButton.setLayoutData(gridData);
		needSignButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				needSign = !needSign;
			}

		});

		return panel;

	}

	protected boolean isResizable() {
		return true;
	}

	protected void okPressed() {
		if (this.targetActivityCombo.getSelection() == null)
			return;

		super.okPressed();
	}

	public boolean getNeedSign() {
		return this.needSign;
	}

	public String getTargetActivityId() {
		return targetActivityId;
	}

	public String getNextActorIds() {
		return nextActorIds;
	}
}

class SimulatorToolBar implements ISelectionChangedListener {
	ToolBar toolBar = null;
	ToolItem run = null;
	ToolItem sign_workitem = null;
	ToolItem complete_workitem = null;
	ToolItem jumpTo = null;
	// ToolItem loopTo = null;
	ToolItem withdraw = null;
	ToolItem reject = null;

	ToolItem stop = null;
	ToolItem set_var = null;
	ToolItem clear = null;
	ComboViewer workflowProcessListViewer = null;

	Menu sign_workitem_menu = null;
	Menu complete_workitem_menu = null;
	Menu jumpTo_menu = null;
	Menu withdraw_menu = null;
	Menu reject_menu = null;
	// Menu loopTo_menu = null;

	Composite toolbarPanel = null;
	SimulatorPanel simulatorPanel = null;

	Composite parent = null;

	public SimulatorToolBar(Composite parent, SimulatorPanel arg0) {
		this.parent = parent;
		simulatorPanel = arg0;
		initToolBar(parent);
		initPopupMenu(toolBar);
		initToolBarItem();
		disableAll();
	}

	public void initPopupMenu(final ToolBar tlbar) {
		/*
		sign_workitem_menu = new Menu(tlbar);
		MenuItem menuItem = new MenuItem(sign_workitem_menu, SWT.PUSH);
		menuItem.setText("Claim as Fireflow simulator");
		ImageDescriptor smallIcon = ImageDescriptor.createFromFile(
				Activator.class, "resources/sign_workitem_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				acceptWorkItem(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				acceptWorkItem(FireflowSimulator.DEFAUT_ACTOR);
			}

		});

		menuItem = new MenuItem(sign_workitem_menu, SWT.PUSH);
		menuItem.setText("Claim as ...");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/sign_workitem_as_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					acceptWorkItem(actorId);
				}

			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					acceptWorkItem(actorId);
				}
			}

		});

		complete_workitem_menu = new Menu(tlbar);
		menuItem = new MenuItem(complete_workitem_menu, SWT.PUSH);
		menuItem.setText("Complete as Fireflow simulator");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/complete_workitem_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				completeWorkitem(FireflowSimulator.DEFAUT_ACTOR,true);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				completeWorkitem(FireflowSimulator.DEFAUT_ACTOR,true);
			}

		});

		menuItem = new MenuItem(complete_workitem_menu, SWT.PUSH);
		menuItem.setText("Complete as ...");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/complete_workitem_as_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					completeWorkitem(actorId,true);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					completeWorkitem(actorId,true);
				}
			}

		});

		jumpTo_menu = new Menu(tlbar);
		menuItem = new MenuItem(jumpTo_menu, SWT.PUSH);
		menuItem.setText("Jump to as Fireflow simulator");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/jumpTo_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				jumpTo(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				jumpTo(FireflowSimulator.DEFAUT_ACTOR);
			}

		});

		menuItem = new MenuItem(jumpTo_menu, SWT.PUSH);
		menuItem.setText("Jump to as ...");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/jumpTo_as_16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					jumpTo(actorId);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					jumpTo(actorId);
				}
			}

		});

		withdraw_menu = new Menu(tlbar);
		menuItem = new MenuItem(withdraw_menu, SWT.PUSH);
		menuItem.setText("Withdraw as Fireflow simulator");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/withdraw16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				withdraw(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				withdraw(FireflowSimulator.DEFAUT_ACTOR);
			}

		});

		menuItem = new MenuItem(withdraw_menu, SWT.PUSH);
		menuItem.setText("Withdraw as ...");
		// smallIcon = ImageDescriptor.createFromFile(Activator.class,
		// "resources/complete_workitem_as_16.gif");
		// menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					withdraw(actorId);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					withdraw(actorId);
				}
			}

		});

		reject_menu = new Menu(tlbar);
		menuItem = new MenuItem(reject_menu, SWT.PUSH);
		menuItem.setText("Reject as Fireflow simulator");
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/reject16.gif");
		menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				withdraw(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				withdraw(FireflowSimulator.DEFAUT_ACTOR);
			}

		});

		menuItem = new MenuItem(reject_menu, SWT.PUSH);
		menuItem.setText("Reject as ...");
		// smallIcon = ImageDescriptor.createFromFile(Activator.class,
		// "resources/complete_workitem_as_16.gif");
		// menuItem.setImage(smallIcon.createImage());
		menuItem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					reject(actorId);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				String actorId = getActorId();
				if (actorId != null && !actorId.trim().equals("")) {
					reject(actorId);
				}
			}

		});
		*/
	}

	public Control getControl() {
		return toolbarPanel;
	}

	protected void initToolBar(Composite parent) {
		toolbarPanel = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, false);
		layout.marginHeight = 0;
		toolbarPanel.setLayout(layout);
		toolBar = new ToolBar(toolbarPanel, SWT.HORIZONTAL | SWT.WRAP
				| SWT.FLAT);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		toolBar.setLayoutData(gridData);

		workflowProcessListViewer = new ComboViewer(toolbarPanel, SWT.DROP_DOWN);
		workflowProcessListViewer
				.setContentProvider(new ArrayContentProvider());
		workflowProcessListViewer.setLabelProvider(new LabelProvider() {
			public String getText(Object element) {
				SubProcessWrapper wrapper = (SubProcessWrapper) element;
				String name = wrapper.getName();
				String displayName = wrapper.getDisplayName();
				if (displayName == null)
					displayName = "";
				return (!displayName.trim().equals("")) ? displayName : name;
			}
		});
		gridData = new GridData();
		workflowProcessListViewer.getControl().setLayoutData(gridData);
		workflowProcessListViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {

					public void selectionChanged(SelectionChangedEvent arg0) {
						// TODO Auto-generated method stub
						IStructuredSelection selection = (IStructuredSelection) workflowProcessListViewer
								.getSelection();

						simulatorPanel
								.setCurrentWorkflowProcessWrapper((SubProcessWrapper) selection
										.getFirstElement());

						simulatorPanel
								.colorEditPart(((SubProcessWrapper) selection
										.getFirstElement()).getID());
					}

				});

		Button button = new Button(toolbarPanel, SWT.FLAT);
		button.setText("About");
		button.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void widgetSelected(SelectionEvent arg0) {

				MessageDialog.openInformation(toolbarPanel.getShell(), "Info",
						"Simulator based on \n\n Engine version:"
								+ Version.getEngineVersion() + "\n"
								+ "Model version:" + Version.getModelVersion());

			}

		});

	}

	protected void initToolBarItem() {
		/*
		run = new ToolItem(toolBar, SWT.PUSH);
		ImageDescriptor smallIcon = ImageDescriptor.createFromFile(
				Activator.class, "resources/run_step_by_step_16.gif");
		run.setImage(smallIcon.createImage());
		run.setToolTipText("Create workflow process instance.");
		run.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				startWorkflowProcess();
			}

			public void widgetSelected(SelectionEvent arg0) {
				startWorkflowProcess();

			}

			private void startWorkflowProcess() {
				// 首先判断是否已经保存，
				if (simulatorPanel.parentEditor.isDirty()) {
					MessageDialog.openError(toolBar.getShell(), "Error",
							"Please save the workflow process first.");
					return;
				}
				IFile file = ((IFileEditorInput) simulatorPanel.parentEditor
						.getEditorInput()).getFile();
				String fileFullName = file.getFullPath() + File.pathSeparator
						+ file.getName();

				ProcessInstance newProcessInstance = null;
//				          simulatorPanel.simulator
//						.run(
//								(WorkflowProcess) simulatorPanel.mainWorkflowProcessWrapper
//										.getWorkflowModelElement(),
//								fileFullName);

				if (newProcessInstance != null) {
					simulatorPanel.process_processInstance_map.put(
							simulatorPanel.currerntWorkflowProcessWrapper
									.getID(), newProcessInstance);
				}

				run.setEnabled(false);
				ISelection selection = simulatorPanel.getSelection();
				if (selection instanceof StructuredSelection) {
					refresh((StructuredSelection) selection);
				}
			}

		});

		sign_workitem = new ToolItem(toolBar, SWT.DROP_DOWN);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/sign_workitem_16.gif");
		sign_workitem.setImage(smallIcon.createImage());
		sign_workitem.setToolTipText("Claim workitem as Fireflow Simulator");

		sign_workitem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(sign_workitem));
				} else {
					acceptWorkItem(FireflowSimulator.DEFAUT_ACTOR);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(sign_workitem));
				} else {
					acceptWorkItem(FireflowSimulator.DEFAUT_ACTOR);
				}
			}

			public void showDropDownMenu(Point p) {
				sign_workitem_menu.setLocation(p);
				sign_workitem_menu.setVisible(true);
			}
		});

		complete_workitem = new ToolItem(toolBar, SWT.DROP_DOWN);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/complete_workitem_16.gif");
		complete_workitem.setImage(smallIcon.createImage());
		complete_workitem
				.setToolTipText("Complete workitem as Fireflow Simulator");
		complete_workitem.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(complete_workitem));
				} else {
					completeWorkitem(FireflowSimulator.DEFAUT_ACTOR,false);
				}
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(complete_workitem));
				} else {
					completeWorkitem(FireflowSimulator.DEFAUT_ACTOR,false);
				}
			}

			private void showDropDownMenu(Point p) {
				complete_workitem_menu.setLocation(p);
				complete_workitem_menu.setVisible(true);
			}

		});

		jumpTo = new ToolItem(toolBar, SWT.DROP_DOWN);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/jumpTo_16.gif");
		jumpTo.setImage(smallIcon.createImage());
		jumpTo.setToolTipText("Jump to as Fireflow Simulator");
		jumpTo.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				jumpTo(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(jumpTo));
				} else {
					jumpTo(FireflowSimulator.DEFAUT_ACTOR);
				}
			}

			private void showDropDownMenu(Point p) {
				jumpTo_menu.setLocation(p);
				jumpTo_menu.setVisible(true);
			}
		});


		withdraw = new ToolItem(toolBar, SWT.DROP_DOWN);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/withdraw16.gif");
		withdraw.setImage(smallIcon.createImage());
		withdraw.setToolTipText("Withdraw workitem as Fireflow Simulator");
		withdraw.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				withdraw(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(jumpTo));
				} else {
					withdraw(FireflowSimulator.DEFAUT_ACTOR);
				}
			}

			private void showDropDownMenu(Point p) {
				withdraw_menu.setLocation(p);
				withdraw_menu.setVisible(true);
			}

		});

		reject = new ToolItem(toolBar, SWT.DROP_DOWN);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/reject16.gif");
		reject.setImage(smallIcon.createImage());
		reject.setToolTipText("Reject workitem as Fireflow Simulator");
		reject.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				reject(FireflowSimulator.DEFAUT_ACTOR);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.detail == 4) {
					showDropDownMenu(Util4Eclipse
							.getPopupMenuLocation4ToolItem(jumpTo));
				} else {
					reject(FireflowSimulator.DEFAUT_ACTOR);
				}
			}

			private void showDropDownMenu(Point p) {
				reject_menu.setLocation(p);
				reject_menu.setVisible(true);
			}

		});

		stop = new ToolItem(toolBar, SWT.PUSH);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/stop_16.gif");
		stop.setImage(smallIcon.createImage());
		stop.setToolTipText("Abort process instance");

		stop.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
//				simulatorPanel.simulator
//						.abortProcessInstance(simulatorPanel.currentProcessInstance);
			}

			public void widgetSelected(SelectionEvent arg0) {
//				simulatorPanel.simulator
//						.abortProcessInstance(simulatorPanel.currentProcessInstance);
			}

		});

		ToolItem serperator = new ToolItem(toolBar, SWT.SEPARATOR);

		set_var = new ToolItem(toolBar, SWT.PUSH);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/datafield16.gif");
		set_var.setToolTipText("Set process instance variable");
		set_var.setImage(smallIcon.createImage());
		set_var.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				VarDialog varDialog = new VarDialog(toolBar.getShell());
				varDialog.open();
				String varName = varDialog.getVarName();
				Object varValue = varDialog.getVarValue();
				if (varName != null) {
//					simulatorPanel.simulator.setWorkflowVariable(
//							simulatorPanel.currentProcessInstance, varName,
//							varValue);
				}
			}

		});

		ToolItem serperator2 = new ToolItem(toolBar, SWT.SEPARATOR);

		clear = new ToolItem(toolBar, SWT.PUSH);
		smallIcon = ImageDescriptor.createFromFile(Activator.class,
				"resources/clear_16.gif");
		clear.setImage(smallIcon.createImage());
		clear.setToolTipText("Clear all simulation data");
		clear.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				simulatorPanel.simulator.clearAllSimulationData();
				simulatorPanel.refresh();
				refresh(null);
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				simulatorPanel.simulator.clearAllSimulationData();
				simulatorPanel.refresh();
				refresh(null);
			}

		});

		ToolItem serperator3 = new ToolItem(toolBar, SWT.SEPARATOR);

		// workflowViewer.setControl(combo2);
		// workflowViewer.setWidth(combo2.getBounds().width);

		// return toolbar;
		 
		 */
	}

	private String getActorId() {
		InputDialog inputDlg = new InputDialog(toolBar.getShell(), "",
				"Please Input the Actor Id of current workitem", null, null);
		inputDlg.open();
		return inputDlg.getValue();
	}

	protected void acceptWorkItem(String actorId) {
		/*
		ISelection selection = simulatorPanel.getSelection();
		if (selection instanceof StructuredSelection) {
			StructuredSelection structuredSelection = (StructuredSelection) selection;
			EditPart editPart = (EditPart) structuredSelection
					.getFirstElement();
			if (editPart instanceof TaskPart4Simulator) {
				TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
				Task task = (Task) taskWrapper.getWorkflowModelElement();
				simulatorPanel.simulator.claimWorkItem4Task(
						simulatorPanel.currerntWorkflowProcessWrapper.getID(),
						task.getId(), actorId);
			} else if (editPart instanceof TaskRefPart4Simulator) {
				TaskRefWrapper taskRefWrapper = (TaskRefWrapper) editPart
						.getModel();
				TaskRef taskRef = (TaskRef) taskRefWrapper
						.getWorkflowModelElement();
				simulatorPanel.simulator.claimWorkItem4Task(
						simulatorPanel.currerntWorkflowProcessWrapper.getID(),
						taskRef.getReferencedTask().getId(), actorId);
			}
			refresh(structuredSelection);
		}
		*/
	}

	protected void completeWorkitem(String thisActorId,
			boolean setTheNextActorId) {
		/*
		String nextActorIdsList = null;
		if (setTheNextActorId) {
			InputDialog inputDlg = new InputDialog(toolBar.getShell(), "",
					"Please Input the Actor Ids for the Next Activity",
					FireflowSimulator.DEFAUT_ACTOR, null);
			inputDlg.open();
			nextActorIdsList = inputDlg.getValue();

			if (inputDlg.getReturnCode() != InputDialog.OK)
				return;
		}
		ISelection selection = simulatorPanel.getSelection();
		if (selection instanceof StructuredSelection) {
			StructuredSelection structuredSelection = (StructuredSelection) selection;
			EditPart editPart = (EditPart) structuredSelection
					.getFirstElement();
			if (editPart instanceof TaskPart4Simulator) {
				TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
				Task task = (Task) taskWrapper.getWorkflowModelElement();
				simulatorPanel.simulator.completeWorkItem4Task(
						simulatorPanel.currerntWorkflowProcessWrapper.getID(),
						task.getId(), thisActorId, nextActorIdsList);
			} else if (editPart instanceof TaskRefPart4Simulator) {
				TaskRefWrapper taskRefWrapper = (TaskRefWrapper) editPart
						.getModel();
				TaskRef taskRef = (TaskRef) taskRefWrapper
						.getWorkflowModelElement();
				simulatorPanel.simulator.completeWorkItem4Task(
						simulatorPanel.currerntWorkflowProcessWrapper.getID(),
						taskRef.getReferencedTask().getId(), thisActorId,
						nextActorIdsList);
			}
			refresh(structuredSelection);
		}
		*/
	}

	private void withdraw(String actorId) {
		/*
		ISelection selection = simulatorPanel.getSelection();
		if (!(selection instanceof StructuredSelection))
			return;
		StructuredSelection structuredSelection = (StructuredSelection) selection;
		EditPart editPart = (EditPart) structuredSelection.getFirstElement();
		if (editPart instanceof TaskPart4Simulator) {

			TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
			Task task = (Task) taskWrapper.getWorkflowModelElement();
			simulatorPanel.simulator.withdraw(actorId,
					simulatorPanel.currentProcessInstance.getProcessId(), task
							.getId());
		} else if (editPart instanceof TaskRefPart4Simulator) {
			TaskRefWrapper taskRefWrapper = (TaskRefWrapper) editPart
					.getModel();
			TaskRef taskRef = (TaskRef) taskRefWrapper
					.getWorkflowModelElement();
			simulatorPanel.simulator.withdraw(actorId,
					simulatorPanel.currentProcessInstance.getProcessId(),
					taskRef.getReferencedTask().getId());
		}
		*/
	}

	private void reject(String actorId) {
		/*
		ISelection selection = simulatorPanel.getSelection();
		if (!(selection instanceof StructuredSelection))
			return;
		StructuredSelection structuredSelection = (StructuredSelection) selection;
		EditPart editPart = (EditPart) structuredSelection.getFirstElement();
		if (editPart instanceof TaskPart4Simulator) {
			TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
			Task task = (Task) taskWrapper.getWorkflowModelElement();
			simulatorPanel.simulator.reject(actorId,
					simulatorPanel.currentProcessInstance.getProcessId(), task
							.getId());
		} else if (editPart instanceof TaskRefPart4Simulator) {
			TaskRefWrapper taskRefWrapper = (TaskRefWrapper) editPart
					.getModel();
			TaskRef taskRef = (TaskRef) taskRefWrapper
					.getWorkflowModelElement();
			simulatorPanel.simulator.reject(actorId,
					simulatorPanel.currentProcessInstance.getProcessId(),
					taskRef.getReferencedTask().getId());
		}
		*/
	}

	private void jumpTo(String actorId) {
		/*
		ISelection selection = simulatorPanel.getSelection();
		if (!(selection instanceof StructuredSelection))
			return;

		JumpToDialog dialog = new JumpToDialog(toolBar.getShell(),
				(WorkflowProcess) simulatorPanel.currerntWorkflowProcessWrapper
						.getWorkflowModelElement(), false);
		dialog.open();
		if (dialog.getReturnCode() != Dialog.OK)
			return;
		String targetActivityId = dialog.getTargetActivityId();
		String nextActorIdsStr = dialog.getNextActorIds();
		boolean needSign = dialog.getNeedSign();
		List<String> nextActorIdsList = null;
		if (nextActorIdsStr != null && !nextActorIdsStr.trim().equals("")) {
			nextActorIdsList = new ArrayList<String>();
			StringTokenizer tokenizer = new StringTokenizer(nextActorIdsStr,
					",");
			while (tokenizer.hasMoreElements()) {
				nextActorIdsList.add(tokenizer.nextToken());
			}
		}

		StructuredSelection structuredSelection = (StructuredSelection) selection;
		EditPart editPart = (EditPart) structuredSelection.getFirstElement();
		if (editPart instanceof TaskPart4Simulator) {
			TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
			Task task = (Task) taskWrapper.getWorkflowModelElement();

			simulatorPanel.simulator.jumpTo(
					simulatorPanel.currerntWorkflowProcessWrapper.getID(), task
							.getId(), actorId, targetActivityId,
					nextActorIdsList, needSign);
		} else if (editPart instanceof TaskRefPart4Simulator) {
			TaskRefWrapper taskRefWrapper = (TaskRefWrapper) editPart
					.getModel();
			TaskRef taskRef = (TaskRef) taskRefWrapper
					.getWorkflowModelElement();

			simulatorPanel.simulator.jumpTo(
					simulatorPanel.currerntWorkflowProcessWrapper.getID(),
					taskRef.getReferencedTask().getId(), actorId,
					targetActivityId, nextActorIdsList, needSign);
		}
		*/
	}

	/**
	 * @deprecated
	 * @param actorId
	 */
	private void loopTo(String actorId) {
		/*
		ISelection selection = simulatorPanel.getSelection();
		if (!(selection instanceof StructuredSelection))
			return;
		StructuredSelection structuredSelection = (StructuredSelection) selection;
		EditPart editPart = (EditPart) structuredSelection.getFirstElement();
		if (!(editPart instanceof TaskPart4Simulator))
			return;
		TaskWrapper taskWrapper = (TaskWrapper) editPart.getModel();
		Task task = (Task) taskWrapper.getWorkflowModelElement();

		JumpToDialog dialog = new JumpToDialog(toolBar.getShell(),
				(WorkflowProcess) simulatorPanel.currerntWorkflowProcessWrapper
						.getWorkflowModelElement(), true);
		dialog.open();
		if (dialog.getReturnCode() != Dialog.OK)
			return;
		String targetActivityId = dialog.getTargetActivityId();
		String nextActorIdsStr = dialog.getNextActorIds();
		boolean needSign = dialog.getNeedSign();
		List nextActorIdsList = null;
		if (nextActorIdsStr != null && !nextActorIdsStr.trim().equals("")) {
			nextActorIdsList = new ArrayList();
			StringTokenizer tokenizer = new StringTokenizer(nextActorIdsStr,
					",");
			while (tokenizer.hasMoreElements()) {
				nextActorIdsList.add(tokenizer.nextToken());
			}
		}
		simulatorPanel.simulator.loopTo(
				simulatorPanel.currerntWorkflowProcessWrapper.getID(), task
						.getId(), actorId, targetActivityId);
		*/
	}

	public void selectionChanged(SelectionChangedEvent arg0) {
		// TODO Auto-generated method stub
		StructuredSelection selection = (StructuredSelection) arg0
				.getSelection();
		refresh(selection);
	}

	private void disableAll() {
		/*
		// run.setEnabled(false);
		sign_workitem.setEnabled(false);
		complete_workitem.setEnabled(false);
		jumpTo.setEnabled(false);
		// loopTo.setEnabled(false);
		stop.setEnabled(false);
		set_var.setEnabled(false);
		withdraw.setEnabled(false);
		reject.setEnabled(false);
		*/
	}

	public void refresh(StructuredSelection selection) {
		/*
		disableAll();
		IPersistenceService persistenceService = simulatorPanel.simulator
				.getRuntimeContext().getPersistenceService();

		List processInstanceList = persistenceService
				.findProcessInstancesByProcessId(simulatorPanel.currerntWorkflowProcessWrapper
						.getID());
		if (processInstanceList != null && processInstanceList.size() > 0) {
			set_var.setEnabled(true);
			stop.setEnabled(true);
		} else {
			run.setEnabled(true);
		}

		List workitemList = null;
		if (selection != null) {
			EditPart editPart = (EditPart) selection.getFirstElement();
			AbsWrapper modelWrapper = (AbsWrapper) editPart
					.getModel();

			if (modelWrapper != null && (modelWrapper instanceof TaskWrapper)) {
				TaskWrapper taskWrapper = (TaskWrapper) modelWrapper;
				workitemList = persistenceService
						.findWorkItemsForTask(taskWrapper.getID());
			}
			if (modelWrapper != null
					&& (modelWrapper instanceof TaskRefWrapper)) {
				TaskRef taskRef = (TaskRef) ((TaskRefWrapper) modelWrapper)
						.getWorkflowModelElement();
				workitemList = persistenceService.findWorkItemsForTask(taskRef
						.getReferencedTask().getId());
			}
			IWorkItem wi = null;
			boolean withdrawEnabled = true;
			for (int i = 0; workitemList != null && i < workitemList.size(); i++) {
				wi = (IWorkItem) workitemList.get(i);

				if (wi.getState() != wi.COMPLETED
						&& wi.getState() != wi.CANCELED) {
					if (wi.getState() == wi.INITIALIZED) {
						sign_workitem.setEnabled(true);
					} else {
						complete_workitem.setEnabled(true);
						jumpTo.setEnabled(true);
					}
					reject.setEnabled(true);
					withdrawEnabled = false;
				}
			}
			if (withdrawEnabled && workitemList != null
					&& workitemList.size() > 0) {
				withdraw.setEnabled(true);
			}

		}
		*/
	}

	public void refreshWorkflowProcessListViewer(List workflowProcesWrapperList) {
		workflowProcessListViewer.setInput(workflowProcesWrapperList);
	}

	public void setCurrentWorkflowProcessIndex(int idx) {
		((Combo) workflowProcessListViewer.getControl()).select(idx);
	}
}
